import { ref } from 'vue'

/**
 * 转义/反转义功能组合式函数
 */
export function useEscapeConverter() {
  const isProcessing = ref(false)

  // Java字符串转义
  const escapeJava = (str: string): string => {
    return str
      .replace(/\\/g, '\\\\')
      .replace(/"/g, '\\"')
      .replace(/'/g, "\\'") 
      .replace(/\n/g, '\\n')
      .replace(/\r/g, '\\r')
      .replace(/\t/g, '\\t')
      .replace(/\b/g, '\\b')
      .replace(/\f/g, '\\f')
  }

  // Java字符串反转义
  const unescapeJava = (str: string): string => {
    return str
      .replace(/\\\\/g, '\\')
      .replace(/\\"/g, '"')
      .replace(/\\'/g, "'")
      .replace(/\\n/g, '\n')
      .replace(/\\r/g, '\r')
      .replace(/\\t/g, '\t')
      .replace(/\\b/g, '\b')
      .replace(/\\f/g, '\f')
  }

  // JavaScript字符串转义
  const escapeJs = (str: string): string => {
    return str
      .replace(/\\/g, '\\\\')
      .replace(/"/g, '\\"')
      .replace(/'/g, "\\'") 
      .replace(/`/g, '\\`')
      .replace(/\n/g, '\\n')
      .replace(/\r/g, '\\r')
      .replace(/\t/g, '\\t')
      .replace(/\b/g, '\\b')
      .replace(/\f/g, '\\f')
      .replace(/\v/g, '\\v')
      .replace(/\0/g, '\\0')
  }

  // JavaScript字符串反转义
  const unescapeJs = (str: string): string => {
    return str
      .replace(/\\\\/g, '\\')
      .replace(/\\"/g, '"')
      .replace(/\\'/g, "'")
      .replace(/\\`/g, '`')
      .replace(/\\n/g, '\n')
      .replace(/\\r/g, '\r')
      .replace(/\\t/g, '\t')
      .replace(/\\b/g, '\b')
      .replace(/\\f/g, '\f')
      .replace(/\\v/g, '\v')
      .replace(/\\0/g, '\0')
  }

  // HTML实体转义
  const escapeHtml = (str: string): string => {
    const htmlEntities: Record<string, string> = {
      '&': '&amp;',
      '<': '&lt;',
      '>': '&gt;',
      '"': '&quot;',
      "'": '&#39;',
      '/': '&#x2F;'
    }
    
    return str.replace(/[&<>"'\/]/g, (char) => htmlEntities[char] || char)
  }

  // HTML实体反转义
  const unescapeHtml = (str: string): string => {
    const htmlEntities: Record<string, string> = {
      '&amp;': '&',
      '&lt;': '<',
      '&gt;': '>',
      '&quot;': '"',
      '&#39;': "'",
      '&#x2F;': '/'
    }
    
    return str.replace(/&(amp|lt|gt|quot|#39|#x2F);/g, (match) => htmlEntities[match] || match)
  }

  // JSON字符串转义
  const escapeJson = (str: string): string => {
    return JSON.stringify(str).slice(1, -1)
  }

  // JSON字符串反转义
  const unescapeJson = (str: string): string => {
    try {
      return JSON.parse(`"${str}"`)
    } catch {
      return str
    }
  }

  // XML实体转义
  const escapeXml = (str: string): string => {
    const xmlEntities: Record<string, string> = {
      '&': '&amp;',
      '<': '&lt;',
      '>': '&gt;',
      '"': '&quot;',
      "'": '&apos;'
    }
    
    return str.replace(/[&<>"']/g, (char) => xmlEntities[char] || char)
  }

  // XML实体反转义
  const unescapeXml = (str: string): string => {
    const xmlEntities: Record<string, string> = {
      '&amp;': '&',
      '&lt;': '<',
      '&gt;': '>',
      '&quot;': '"',
      '&apos;': "'"
    }
    
    return str.replace(/&(amp|lt|gt|quot|apos);/g, (match) => xmlEntities[match] || match)
  }

  // Unicode转义
  const escapeUnicode = (str: string): string => {
    return str.replace(/[\u0080-\uFFFF]/g, (match) => {
      return '\\u' + ('0000' + match.charCodeAt(0).toString(16)).substr(-4)
    })
  }

  // Unicode反转义
  const unescapeUnicode = (str: string): string => {
    return str.replace(/\\u([0-9a-fA-F]{4})/g, (match, hex) => {
      return String.fromCharCode(parseInt(hex, 16))
    })
  }

  // 转换函数映射
  const converters: Record<string, (str: string) => string> = {
    escapeJava,
    unescapeJava,
    escapeJs,
    unescapeJs,
    escapeHtml,
    unescapeHtml,
    escapeJson,
    unescapeJson,
    escapeXml,
    unescapeXml,
    escapeUnicode,
    unescapeUnicode
  }

  // 主转换函数
  const convert = (type: string, text: string): string => {
    if (!text) return ''
    
    isProcessing.value = true
    
    try {
      const converter = converters[type]
      if (!converter) {
        throw new Error(`未知的转换类型: ${type}`)
      }
      
      return converter(text)
    } catch (error) {
      throw error
    } finally {
      isProcessing.value = false
    }
  }

  return {
    convert,
    isProcessing
  }
}